﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.ProjectTypeCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.ProjectTypeCommandHander
{
    public class CreateProjectTypeCommandHander : IRequestHandler<CreateProjectTypeCommand, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public CreateProjectTypeCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(CreateProjectTypeCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<ProjectType>(request);

            model.Del = false;

            result.Data = await db.Insertable(model).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = APIEnum.success;
                result.Msg = "添加项目名称成功";
            }
            else
            {
                result.Code = APIEnum.error;
                result.Msg = "添加项目名称失败";
            }

            return result;
        }
    }
}
